package com.sunny.ynhz.ui.ynts.partorlstaticfragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.AreaDBAdapter;
import com.sunny.ynhz.adapter.TimeSelectAdapter;
import com.sunny.ynhz.bean.StandardBean;
import com.sunny.ynhz.bean.TimeBean;
import com.sunny.ynhz.bean.WarnHorBarBean;
import com.sunny.ynhz.model.IProblemTypeResultView;
import com.sunny.ynhz.model.ProblemStandModel;
import com.sunny.ynhz.ui.base.BaseFragment;
import com.sunny.ynhz.utils.ChartUtils;
import com.sunny.ynhz.utils.MoveToPosition;
import com.sunny.ynhz.utils.ShowUtils;
import com.sunny.ynhz.utils.TimeUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by huhuan123 on 2018/8/8.
 *
 * @describe TODO
 */

public class CunStaticFragment extends BaseFragment implements IProblemTypeResultView<List<StandardBean.DateBean>> {
    @BindView(R.id.tv_current_week)
    TextView tv_current_week;
    @BindView(R.id.lv_recycler)
    RecyclerView lv_recycler;
    @BindView(R.id.hor_bar_chart)
    HorizontalBarChart hor_bar_chart;
    @BindView(R.id.rl_db)
    RecyclerView rl_db;
    List<String> mKeyList = new ArrayList<>();
    List<List<String>> mcontentList = new ArrayList<>();
    List<TimeBean> list = new ArrayList<>();
    List<StandardBean.DateBean> mstandardBean = new ArrayList<>();
    private int currentKey = 2;
    private int currentContent = 0;
    private LinearLayoutManager linearLayoutManager;
    private TimeSelectAdapter timeSelectAdapter;
    private ProblemStandModel problemStandModel;
    private AreaDBAdapter areaDBAdapter;
    private String currentYear;
    private String currentMonth;
    private String currentDay;

    public static CunStaticFragment getInstanceCunStaticFragment() {
        return new CunStaticFragment();
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.city_fragment;
    }

    @Override
    protected void setUpView() {
        super.setUpView();
        currentYear = TimeUtils.getSysYear();
        currentMonth = TimeUtils.getCurrentMonth() + "";
        currentDay = TimeUtils.getNowDay();
        tv_current_week.setText(currentYear + "年" + currentMonth + "月" + currentDay + "日");
        initData();
        initCurrentDay(TimeUtils.getSysYear(), TimeUtils.getCurrentMonth() + "", currentDay);
        linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        timeSelectAdapter = new TimeSelectAdapter(list, getContext(), 5);
        lv_recycler.setLayoutManager(linearLayoutManager);
        lv_recycler.setAdapter(timeSelectAdapter);
        MoveToPosition.moveToPositionHor(linearLayoutManager, lv_recycler, Integer.parseInt(currentDay) - 1);
        problemStandModel = new ProblemStandModel(this, getContext());
        problemStandModel.getStandData(TimeUtils.getNowData(), "3");
        areaDBAdapter = new AreaDBAdapter(mstandardBean);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rl_db.setLayoutManager(linearLayoutManager);
        rl_db.setAdapter(areaDBAdapter);
        timeSelectAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < list.size(); i++) {
                    TimeBean timeBean = list.get(i);
                    if (i == position) {
                        timeBean.setCheck(true);
                    } else {
                        timeBean.setCheck(false);
                    }
                }
                currentDay = list.get(position).getTime();
                tv_current_week.setText(currentYear + "年" + currentMonth + "月" + currentDay + "日");

                timeSelectAdapter.notifyDataSetChanged();
                problemStandModel.getStandData(currentYear + "-" + currentMonth + "-" + currentDay, "3");
            }
        });


    }

    @OnClick({R.id.tv_current_week})
    public void onClickView(View view) {
        displayTimeSelect();
    }

    public void displayTimeSelect() {
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        String sysYear = TimeUtils.getSysYear();
        int mCurrentMonth = TimeUtils.getCurrentMonth();
        String nowDate = TimeUtils.getNowDay();
        startDate.set(Integer.parseInt(sysYear) - 2, 0, 1);
        endDate.set(Integer.parseInt(sysYear), mCurrentMonth - 1, Integer.parseInt(nowDate));

        TimePickerView pvTime = new TimePickerBuilder(getContext(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = formatter.format(date);
                currentYear = dateString.split("-")[0];
                currentMonth = dateString.split("-")[1];
                currentDay = dateString.split("-")[2];
                tv_current_week.setText(currentYear + "年" + currentMonth + "月" + currentDay + "日");
                initCurrentDay(currentYear, currentMonth, currentDay);
                timeSelectAdapter.notifyDataSetChanged();
                MoveToPosition.moveToPositionHor(linearLayoutManager, lv_recycler, Integer.parseInt(currentDay) - 1);
                problemStandModel.getStandData(currentYear + "-" + currentMonth + "-" + currentDay, "3");
            }
        }).setDate(TimeUtils.strToCalendar(currentYear + "-" + currentMonth + "-" + currentDay)).setRangDate(startDate, endDate).build();
        pvTime.show();

    }

    public void initData() {
        mKeyList.add((Integer.parseInt(TimeUtils.getSysYear()) - 2) + "");
        mKeyList.add((Integer.parseInt(TimeUtils.getSysYear()) - 1) + "");
        mKeyList.add(TimeUtils.getSysYear());
        for (int i = 0; i < 3; i++) {
            List<String> mList = new ArrayList<>();
            String year = mKeyList.get(i);
            int maxWeekNumOfYear = TimeUtils.getMaxWeekNumOfYear(Integer.parseInt(year));
            for (int j = 1; j <= maxWeekNumOfYear; j++) {
                mList.add(j + "");
            }
            mcontentList.add(mList);
        }
    }


    private void initCurrentDay(String year, String currentMonth, String currentDay) {
        list.clear();
        int maxWeekNumOfYear = TimeUtils.getDayOfMonth(Integer.parseInt(year), Integer.parseInt(currentMonth));
        for (int i = 1; i <= maxWeekNumOfYear; i++) {
            TimeBean timeBean = new TimeBean();
            if (i == Integer.parseInt(currentDay)) {
                timeBean.setCheck(true);
            } else {
                timeBean.setCheck(false);
            }
            timeBean.setTime(i + "");
            if ((Integer.parseInt(year) - Integer.parseInt(TimeUtils.getSysYear()) == 0) && Integer.parseInt(currentMonth) - TimeUtils.getCurrentMonth() == 0 && i > Integer.parseInt(TimeUtils.getNowDay())) {
                timeBean.setExit(false);

            } else {
                timeBean.setExit(true);
            }
            list.add(timeBean);
        }

    }


    @Override
    public void onSuccess(List<StandardBean.DateBean> dateBeans) {
        loadSuccess();
        ShowUtils.closePopwindow();
        mstandardBean.clear();
        WarnHorBarBean warnData = getWarnData(dateBeans);
        ChartUtils.initHorBar(hor_bar_chart, warnData.getDabiaoList(), warnData.getCityNameList());
        mstandardBean.addAll(dateBeans);
        areaDBAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCache() {

    }

    @Override
    public void onFail() {
        if (mstandardBean.size() > 0) {
            ShowUtils.closePopwindow();
        } else {
            loadFail();
        }


    }

    @Override
    public void onStartLoad() {
        if (mstandardBean.size() > 0) {
            ShowUtils.inItPopwin(getContext(), "请等待");
        } else {
            startLoad();
        }


    }

    @Override
    public void resLoad() {
        super.resLoad();
        problemStandModel.getStandData(currentYear + "-" + currentMonth + "-" + currentDay, "3");
    }

    public WarnHorBarBean getWarnData(List<StandardBean.DateBean> dateBeans) {
        WarnHorBarBean warnHorBarBean = new WarnHorBarBean();
        ArrayList<String> mlist = new ArrayList<>();
        ArrayList<Float> mDaBiaoList = new ArrayList<>();
        for (int i = 0; i < dateBeans.size(); i++) {
            mlist.add(dateBeans.get(i).getADDVNM());
            mDaBiaoList.add(dateBeans.get(i).getVILLAGE_PERCENT());

        }
        warnHorBarBean.setCityNameList(mlist);
        warnHorBarBean.setDabiaoList(mDaBiaoList);
        return warnHorBarBean;
    }
}

